
#include "../include/_DSOGI_PNSC_FLL_PLL.h"

#include <vector>
#include <list>
#include <iostream>
#include <tuple>
#include <utility>
#include <algorithm>
#include <cmath>

#ifdef _win64系统
#include <windows.h>
#else
#include <ctime>
#endif

using namespace std;

#define π 3.141592653589793238462643383279

#define 步长 100 //us
#define 周期 20 //ms
#define 周期数 8



auto  abc2αβ( double a,double b,double c )
{
    auto α =  2/3.0*a   -1/3.0*b      -1/3.0*c        ;
    auto β =         1/sqrt(3.0)*b   -1/sqrt(3.0)*c   ;     
    return std::make_tuple(α,β);     
}



int main()
{   

    // std::vector<float> theta;
    // std::vector<float> A;
    // std::vector<float> f;
    const int 点数 = 周期*1000*周期数/步长;
    float θ_pll[点数];        
    float f_pll[点数];
    float A_pll[点数];     
    float f_fll[点数];
    float α[点数];
    float β[点数];
    float ua[点数];
    float ub[点数];
    float uc[点数];
    void *p1 = DSOGI_PNSC_FLL_PLL_Creat();
    void *p2 = DSOGI_PNSC_FLL_PLL_Creat();
    for (int i=0 ; i<点数; i ++){
        ua[i] = 100*cos( i*2*π/(周期*1000.0/(double)步长)  );
        ub[i] = 100*cos( i*2*π/(周期*1000.0/(double)步长) -2*π/3.0 );
        uc[i] = 100*cos( i*2*π/(周期*1000.0/(double)步长) +2*π/3.0 );
        std::tie( α[i] , β[i] ) = abc2αβ( ua[i] , ub[i] , uc[i] );
    }


    // 计时
    #ifdef _win64系统
    LARGE_INTEGER t1,t2,tc;
    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);
    #else
    clock_t time_start = clock();
#endif
    // 计时👆

    for (int i=0 ; i<点数; i ++){
        //auto usin = sin( i*2*π/(周期*1000.0/(double)步长)  );
        float a1,a2,a3,a4,a5,a6;//暂时不用
        DSOGI_PNSC_FLL_PLL_Cal(
            (DSOGI_PNSC_FLL_PLL *)p1,
            α[i], β[i], 55,
            (float)步长 / 1e6,
            0.01, 2 * π * 50, -2 * π * 50,
            1.45, 46,
            36.16, 94, 3658,
            &θ_pll[i], &A_pll[i], &f_pll[i],
            &f_fll[i],
            &a1, &a2,
            &a3, &a4,
            &a5, &a6,
            0);
    }

    // 计时
    #ifdef _win64系统
    QueryPerformanceCounter(&t2);
    auto time=(double)(t2.QuadPart-t1.QuadPart)/(double)tc.QuadPart;     
    cout << "点数：" << 点数 <<endl;
    cout<<"time =  "<<1000.0*time<<"  ms"<<endl;  //输出时间（单位：ｓ）
    cout<<endl;
    cout<<endl;
    #else
    clock_t time_end = clock();
    cout << "点数：" << 点数 <<endl;
    cout << "time =  " << 1000.0 * (time_end - time_start) / (double)CLOCKS_PER_SEC << "  ms" << endl; //输出时间（单位：ｓ）
    cout << endl;
    cout << endl;    
#endif
    // 计时👆

    for(int i=点数*0 ; i<点数; i ++){
        cout << "输入a相：" << ua[i]
             << "  相位pll:" << θ_pll[i] << "  幅值pll:" << A_pll[i] 
             << "  频率pll:" << f_pll[i] << "  频率fll:" << f_fll[i]
             << endl;
    }
    
    

    system("pause");

}
